package gcb.metalsys.auth.face;

import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.util.Assert;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author ReMidDream
 * @date 2018-09-10 19:39
 **/
public class FaceAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
    // ~ Static fields/initializers
    // =====================================================================================

    public static final String IMAGE_URL = "imageUrl";

    public static final String USER_NAME = "userName";

    private String urlParameter = IMAGE_URL;

    private String userParameter = USER_NAME ;

    private boolean postOnly = true;

    // ~ Constructors
    // ===================================================================================================

    public FaceAuthenticationFilter() {
        //处理 /authentication/mobile 的POST请求
        super(new AntPathRequestMatcher("/faceCheck", "POST"));
    }

    // ~ Methods
    // ========================================================================================================

    public Authentication attemptAuthentication(HttpServletRequest request,
                                                HttpServletResponse response) throws AuthenticationException {
        if (postOnly && !request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException(
                    "Authentication method not supported: " + request.getMethod());
        }

        String imageUrl = obtainImage(request);
        String username = obtainUsername(request);



        FaceAuthenticationToken authRequest = new FaceAuthenticationToken(username,imageUrl);

        // Allow subclasses to set the "details" property
        setDetails(request, authRequest);

        return this.getAuthenticationManager().authenticate(authRequest);
    }


    protected String obtainImage(HttpServletRequest request) {
        return request.getParameter(urlParameter);
    }

    protected String obtainUsername(HttpServletRequest request) {
        return request.getParameter(userParameter);
    }

    protected void setDetails(HttpServletRequest request,
                              FaceAuthenticationToken authRequest) {
        authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
    }


    public void setPostOnly(boolean postOnly) {
        this.postOnly = postOnly;
    }

    public String getUrlParameter() {
        return urlParameter;
    }

    public void setUrlParameter(String urlParameter) {
        this.urlParameter = urlParameter;
    }

    public String getUserParameter() {
        return userParameter;
    }

    public void setUserParameter(String userParameter) {
        this.userParameter = userParameter;
    }
}
